package zhang.lao.service.weather;/**
 * Created by zhangqin on 2017/11/2.
 */

import com.alibaba.fastjson.JSON;
import com.arronlong.httpclientutil.HttpClientUtil;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.arronlong.httpclientutil.common.HttpHeader;
import com.arronlong.httpclientutil.exception.HttpProcessException;
import com.google.common.collect.Maps;
import org.apache.http.Header;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zhang.lao.build.mybatis.jdbc.auto.model.WnWeatherDevice;
import zhang.lao.build.mybatis.jdbc.auto.model.WnWeatherDeviceCurrently;
import zhang.lao.build.mybatis.jdbc.auto.model.WnWeatherDeviceLocation;
import zhang.lao.build.mybatis.jdbc.auto.model.WnWeatherDeviceLog;
import zhang.lao.build.tool.UUIDTool;
import zhang.lao.dao.base.WnWeatherDeviceCurrentlyDao;
import zhang.lao.dao.base.WnWeatherDeviceDao;
import zhang.lao.dao.base.WnWeatherDeviceLocationDao;
import zhang.lao.dao.base.WnWeatherDeviceLogDao;
import zhang.lao.pojo.console.resp.HttpResult;
import zhang.lao.pojo.console.resp.HttpResultUtil;
import zhang.lao.pojo.console.weather.WnWeatherDeviceCurrentlyResp;
import zhang.lao.pojo.console.weather.WnWeatherDeviceLocationResp;
import zhang.lao.pojo.console.weather.WnWeatherDeviceResp;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @create 2017-11-02 11:02
 **/
@Service
public class WeatherService {
    private static final Logger logger = LoggerFactory.getLogger(WeatherService.class);
    @Resource
    private WnWeatherDeviceDao wnWeatherDeviceDao;
    @Resource
    private WnWeatherDeviceLocationDao wnWeatherDeviceLocationDao;
    @Resource
    private WnWeatherDeviceLogDao wnWeatherDeviceLogDao;
    @Resource
    private WnWeatherDeviceCurrentlyDao wnWeatherDeviceCurrentlyDao;


    @Transactional
    public HttpResult logWeather(){

        List<WnWeatherDevice> wnWeatherDevices = wnWeatherDeviceDao.selectByExample(null);
        if(wnWeatherDevices!=null){
            for (WnWeatherDevice wnWeatherDevice : wnWeatherDevices) {
                String url = "https://bskybackend.bloomsky.com/api/currentweather?deviceid="+wnWeatherDevice.getDeviceNumber();
                Map<String, Object> stringStringMap = Maps.newHashMap();
                Header[] headers  = HttpHeader.custom().other("Authorization", ""+wnWeatherDevice.getDeviceAuth()+"")
                        .build();
                //插件式配置请求参数（网址、请求参数、编码、client）
                HttpConfig config = HttpConfig.custom()
                        .headers(headers)	//设置headers，不需要时则无需设置
                        .url(url)					//设置请求的url
                        .map(stringStringMap)			//设置请求参数，没有则无需设置
                        .encoding("utf-8") //设置请求和返回编码，默认就是Charset.defaultCharset()
                        ;
                try {
                    List<WnWeatherDeviceResp> wnWeatherDeviceResps = JSON.parseArray(HttpClientUtil.get(config),WnWeatherDeviceResp.class);
                    for (WnWeatherDeviceResp wnWeatherDeviceResp : wnWeatherDeviceResps) {
                        WnWeatherDeviceCurrentlyResp currently = wnWeatherDeviceResp.getCurrently();
                        WnWeatherDeviceCurrently wnWeatherDeviceCurrently = new WnWeatherDeviceCurrently();
                        BeanUtils.copyProperties(currently,wnWeatherDeviceCurrently);
                        wnWeatherDeviceCurrently.setUptime(currently.getLocaltime());
                        wnWeatherDeviceCurrently.setCreateTime(new Date());
                        wnWeatherDeviceCurrently.setDeviceNumber(wnWeatherDevice.getDeviceNumber());
                        wnWeatherDeviceCurrently.setId(UUIDTool.getUUID());
                        wnWeatherDeviceCurrentlyDao.insertSelective(wnWeatherDeviceCurrently);

                        WnWeatherDeviceLocationResp locationResp = wnWeatherDeviceResp.getLocation();
                        WnWeatherDeviceLocation wnWeatherDeviceLocation = new WnWeatherDeviceLocation();
                        BeanUtils.copyProperties(locationResp,wnWeatherDeviceLocation);
                        wnWeatherDeviceLocation.setId(UUIDTool.getUUID());
                        wnWeatherDeviceLocation.setDeviceNumber(wnWeatherDevice.getDeviceNumber());
                        wnWeatherDeviceLocationDao.insertSelective(wnWeatherDeviceLocation);

                        WnWeatherDeviceLog wnWeatherDeviceLog = new WnWeatherDeviceLog();
                        wnWeatherDeviceLog.setCreateTime(new Date());
                        wnWeatherDeviceLog.setId(UUIDTool.getUUID());
                        wnWeatherDeviceLog.setCurrentlyId(wnWeatherDeviceLocation.getId());
                        wnWeatherDeviceLog.setLocationId(wnWeatherDeviceLocation.getId());
                        wnWeatherDeviceLog.setDeviceNumber(wnWeatherDevice.getDeviceNumber());
                        wnWeatherDeviceLogDao.insertSelective(wnWeatherDeviceLog);
                    }
                    logger.info(JSON.toJSONString(wnWeatherDeviceResps));
                } catch (HttpProcessException e) {
                    e.printStackTrace();
                    logger.error(e.getLocalizedMessage(),e);
                }
            }
        }
        return HttpResultUtil.buildSuccess();
    }
}
